package com.softeem.god.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.god.comm.R;
import com.softeem.god.entity.User;
import com.softeem.god.service.UserService;
import com.softeem.god.mapper.UserMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 86139
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-10-28 11:42:48
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Override
    public R login(String username, String password) {
        //根据用户名查询用户 where username=?
        QueryWrapper<User> query =Wrappers.query(User.class);
        query.eq("username",username);
        //执行查询
        User user = getOne( query);
        if (user==null){
            return R.error().setMsg("用户不存在");
        }
        //账号是否禁用
        if (user.getStatus()==0){
            return R.error().setMsg("账号被禁用");
        }
        //检查密码是否错误
        if (!user.getPassword().equals(password)){
            return R.error().setMsg("密码错误");
        }
        //登陆成功
        return R.success().setData(user);
    }

    @Override
    public R register(User user) {
        //查询是否存在用户
        QueryWrapper<User> query =Wrappers.query(User.class);
        query.eq("username",user.getUsername());
        if (exists(query)){
            return R.error().setMsg("用户已存在");
        }
        if (save(user)){
            return R.success().setData(user);
        }
        return R.error().setMsg("注册失败");
    }
    
    @Override
    public List<User> list(IPage<User> page, QueryWrapper<User> queryWrapper) {
        // 使用MyBatis Plus的分页查询
        IPage<User> result = page(page, queryWrapper);
        // 返回查询结果
        return result.getRecords();
    }
}




